<?php

/**
 * 产品版本号控制器
 *
 * @author wuhaifeng
 */

require_once 'CommonController.php';

class Admin_PartnerProductVersionController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_PartnerProductVersion();
        $this->view->title = "管理中心";
        $this->username = Application_Model_UserLogin::getInstance()->getLoginedUserName();
    }

     /**
     *　产品版本列表
     * 
     * @return void
     */
    public function listAction()
    {
        $partnerProductModel = new Application_Model_PartnerProduct();
        $time = time();
        //获取当前页码
        $pageSize = 20;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();

        $vars['productCode'] = trim($this->_request->get('productCode'));
        $vars['osname'] = trim($this->_request->get('osname'));
        if(empty($vars['productCode']) || empty($vars['osname'])) {
            exit($this->view->message("请求数据错误。", $this->view->projectUrl(array('controller'=>'partner-product', 'action'=>'index'))));
        }
        $urls = $vars;
        $wheres[] = " v.productCode = '" . $vars['productCode'] . "' ";
        $wheres[] = " v.osName = '" . $vars['osname'] . "' ";
        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT v.*,p.name FROM `{$this->_model->getTableName()}` AS v 
                LEFT JOIN `{$partnerProductModel->getTableName()}` AS p 
                ON v.productCode = p.code
                {$where} 
                ORDER BY v.addTime DESC ";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;
        $rows = $dbPaginator->getRows();

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
    }

    /**
     * 修改产品版本信息
     * 
     * @return void
     */
    public function updateAction()
    {
        $vars = array();
        $id = intval($this->_request->get('id'));
        $vars['productCode'] = trim($this->_request->get('productCode'));
        $vars['osname'] = trim($this->_request->get('osname'));
        if (empty($vars['productCode']) || empty($vars['osname'])) {
            exit($this->view->message("请求数据错误。", $this->view->projectUrl(array('controller'=>'partner-product', 'action'=>'index'))));
        }
        $rows = $this->_model->filterData(array('id' => $id));
        if(empty($rows)){
            exit($this->view->message("记录不存在。", $this->view->projectUrl(array('controller'=>'partner-product-version', 'action'=>'list', 'productCode'=>$vars['productCode'], 'osname'=>$vars['osname']))));
        }
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['version'] = $filter->filter(trim($this->_request->getPost('version')));
            if (empty($field['version']) ) {
                echo $this->view->message('请正确填写') ;
                exit;
            }
            $this->_model->update($field, "id = {$id}");
            echo $this->view->message("操作成功。", $this->view->projectUrl(array('controller'=>'partner-product-version', 'action'=>'list', 'productCode'=>$vars['productCode'], 'osname'=>$vars['osname']))) ;
            exit;
        }
        $this->view->vars = $vars;
        $this->view->rows = $rows;
    }

    /**
     * 添加产品版本信息
     * 
     * @return void
     */
    public function insertAction()
    {
        $vars = array();
        $vars['productCode'] = trim($this->_request->get('productCode'));
        $vars['osname'] = trim($this->_request->get('osname'));
        if (empty($vars['productCode']) || empty($vars['osname'])) {
            exit($this->view->message("请求数据错误。", $this->view->projectUrl(array('controller'=>'partner-product', 'action'=>'index'))));
        }
        $this->view->vars = $vars;
        //提交表单的处理　
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['osName'] = $filter->filter(trim($this->_request->getPost('osname')));
            $field['productCode'] = $filter->filter(trim($this->_request->getPost('productcode')));
            $field['version'] = $filter->filter(trim($this->_request->getPost('version')));
            $field['addUserName'] = $this->username;
            $field['addTime'] = date('Y-m-d H:i:s');
            if (empty($field['osName']) || empty($field['productCode']) || empty($field['version']) ) {
                echo $this->view->message('请正确填写') ;
                exit;
            }
            $filterdata = $this->_model->filterData(array('osName' => $field['osName'], 'productCode' => $field['productCode'], 'version' => $field['version']));
            if(!empty($filterdata)) {
                echo $this->view->message('版本号已经存在！') ;
                exit;
            }
            $this->_model->insert($field);
            echo $this->view->message("操作成功。", $this->view->projectUrl(array('controller'=>'partner-product-version', 'action'=>'list', 'productCode'=>$vars['productCode'], 'osname'=>$vars['osname']))) ;
            exit;
        }
    }
 
    
    /**
     * 删除产品版本信息
     * 
     * @return void
     */
    public function deleteAction(){
        $vars = array();
        $vars['productCode'] = trim($this->_request->get('productCode'));
        $vars['osname'] = trim($this->_request->get('osname'));
        if (empty($vars['productCode']) || empty($vars['osname'])) {
            exit($this->view->message("请求数据错误。", $this->view->projectUrl(array('controller'=>'partner-product', 'action'=>'index'))));
        }
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            if (is_array($ids)) {
                $this->_model->delete(" `id` in (" . implode(',', $ids) . ")");
            }
        }
        redirect($this->view->projectUrl(array('controller'=>'partner-product-version', 'action'=>'list', 'productCode'=>$vars['productCode'], 'osname'=>$vars['osname']))) ;
        exit;
    }
}